﻿Option Explicit On
Option Infer On
Option Strict On
Imports Brainworklite.Core.DataHelper
Public Module WinFormsHelper
    Public Class ObjectItem
        Public Text As String
        Public Value As Object
        Public Sub New(ByVal text As String, ByVal value As Object)
            Me.Text = text
            Me.Value = value
        End Sub
    End Class
 
    Public Function getObjectItemCollection(ByVal Ds As DataSet, _
                                            ByVal Text As String, _
                                            ByVal Value As String) As IEnumerable(Of ObjectItem)
        Dim l As New List(Of ObjectItem)
        For Each item As DataRow In Ds.Tables(0).Rows
            l.Add(New ObjectItem(CStr(item(Text)), getNotDBNull(item(Value))))
        Next
        Return l
    End Function

    Public Sub LoadCombo(ByRef cmb As ComboBox, ByVal Ds As DataSet, _
                                            ByVal Text As String, _
                                            ByVal Value As String, _
                                            Optional ByVal [default] As Object = Nothing)
        If cmb.Items IsNot Nothing Then
            cmb.Items.Clear()
        End If

        Dim col = getObjectItemCollection(Ds, Text, Value)

        cmb.DataSource = col
        cmb.DisplayMember = "Text"
        cmb.ValueMember = "Value"
        If [default] IsNot Nothing Then
            Dim itm = col.Where(Function(x) x.Value.Equals([default]))
            If itm.Any Then
                cmb.SelectedItem = itm.FirstOrDefault()
            End If
        End If


    End Sub

    Public Sub LoadCheckBox(ByRef chk As CheckedListBox, ByVal Ds As DataSet, _
                                            ByVal Text As String, _
                                            Optional ByVal [default] As Object = Nothing)
        If chk.Items IsNot Nothing Then
            chk.Items.Clear()
        End If
        If [default] IsNot Nothing Then
            For Each item As DataRow In Ds.Tables(0).Rows
                chk.Items.Add(getNotDBNull(item(Text)), CBool(IIf(getNotDBNull(item(Text)).ToString() = [default].ToString(), True, False)))
            Next
        Else
            For Each item As DataRow In Ds.Tables(0).Rows
                chk.Items.Add(getNotDBNull(item(Text)))
            Next
        End If
       


      

    End Sub


End Module
